package features.advance.jdk.io.unblocking;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

/**
 * @author LIN
 * @date 2021-07-12
 */
public class UdpClient {

    public static void main(String[] args) {
        new Thread(() -> {

            DatagramChannel sendChannel = null;
            try {
                sendChannel = DatagramChannel.open();
                sendChannel.configureBlocking(false);

                InetSocketAddress target = new InetSocketAddress("localhost", 8083);
                DatagramChannel connect = sendChannel.connect(target);


                ByteBuffer wrap = ByteBuffer.wrap("hello world 你好".getBytes());
                sendChannel.write(wrap);
                wrap.flip();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(sendChannel != null){
                    try {
                        sendChannel.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }

        }).start();
    }


}
